package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.engineering.elink.co.PersonCustomCo;
import com.engineering.elink.common.Constant;
import com.engineering.elink.common.ResultType;
import com.engineering.elink.service.PersonCustomService;
import com.engineering.elink.util.MapConvert;

@RestController
@RequestMapping("/api/personCustom")
@Api(value = "个人自定义模块接口")
public class PersonCustomController {
	@Autowired
	private PersonCustomService personCustomService;
	
	/**
	 * 获取个人中心自定义模块列表
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "getPersonCustom", method = RequestMethod.GET)
	@ApiOperation(value = "获取个人中心自定义模块列表",httpMethod="GET",notes="获取个人中心自定义模块列表")
	public Map<String, Object> getPersonCustom(HttpServletRequest request) {
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId") || (int) userInfo.get("userType") != Constant.personUserType) {
			String userId = (String) userInfo.get("userId");
			return MapConvert.getMap(ResultType.SUCCESS, personCustomService.getPersonCustomList(userId));
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}
	
	/**
	 * 更新个人定制模块
	 * @param moduleId
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "modifyCustom", method = RequestMethod.POST)
	@ApiOperation(value = "更新个人定制模块",httpMethod="POST",notes="更新个人定制模块")
	public Map<String, Object> modify(@ApiParam(value="模块id",required=true,defaultValue="0")
	@RequestParam(value="moduleId",required=true,defaultValue="0")int moduleId,HttpServletRequest request){
		Map<String,Object> userInfo  = ((Map<String, Object>) request.getAttribute("userInfo"));
		String userId =(String) userInfo.get("userId");
		return MapConvert.getMap(ResultType.SUCCESS, personCustomService.modify(moduleId,userId));
	}
	
	/**
	 * 排序个人定制模块
	 * @param pcList
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "addCustom", method = RequestMethod.POST)
	@ApiOperation(value = "排序个人定制模块",httpMethod="POST",notes="个人定制模块排序")
	public Map<String, Object>addCustom(
			@ApiParam(value="请用json数组传递数据,[moduleId --模块id,isDisplay--是否显示,0--显示，1--不显示,sequence--排序,数字越高，越显示在前面,isDelete--是否删除,0--不删除，1--删除，默认为0]")
			@RequestParam(value="jsonPc",required=true)String jsonPc,HttpServletRequest request){
		
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId =(String) userInfo.get("userId");
			List<PersonCustomCo> pcList = JSON.parseArray(jsonPc, PersonCustomCo.class); 
			for (PersonCustomCo pc : pcList) {
				pc.setUserId(userId);
			}
			return MapConvert.getMap(ResultType.SUCCESS, personCustomService.addCustom(pcList,userId));
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
}
